var vm = new Vue({
    el: "#app",
    data:{
        host:host,
        img_url:static_url,
        username:'',
        city:'居住地址',
        school:'',
        specialities:'',
        message:"毕业学校",
        company:'',
        position:'',
        message_company:'所在公司',
        website: '填写个人网站',
        intro:'暂时没有个人简介',
        mobile:'',
        email:'添加邮箱',
        file:'',
        image:'img/widget-myphoto.jpg',
        questions:'',  // 提问的问题
        answerses:'',  // 回答的问题
        user_id: sessionStorage.user_id || localStorage.user_id,
        token: sessionStorage.token || localStorage.token,
        error_city: false,
        error_school: false,
        error_company:false,
        error_web:false,
        error_intro:false,
        errors_intro: true,
        error_question: true,
        error_answer:false,

        show_name: false,
        show_web: false,
        show_email: false,
        show_mobile: false,
        show_pwd: false,

        password:'',
        password1:'',
    },
    mounted:function () {

        if (this.user_id && this.token) {
            axios.get(this.host + '/user/information/', {

                headers: {
                    'Authorization': 'JWT ' + this.token
                },
                responseType: 'json'
            })
                .then(response => {
                    this.username = response.data.username
                    this.mobile = response.data.mobile
                    if (response.data.city){
                        this.city = response.data.city
                    }
                    else {
                        this.city = "居住地址"
                    }
                    if (response.data.school){
                        this.school = response.data.school
                        this.specialities = response.data.specialities
                        this.message = this.school + '-' + this.specialities
                    }
                    else {
                        this.message = "毕业学校"
                    }
                    if (response.data.company){
                        this.company = response.data.company
                        this.position = response.data.position
                        this.message_company = this.company + '-' + this.position
                    }
                    else {
                        this.message_company = "所在公司"
                    }
                    if (response.data.website){
                        this.website = response.data.website
                    }
                    else {
                        this.website = '填写个人网站'
                    }
                    if (response.data.intro){
                        this.intro = response.data.intro
                    }
                    else {
                        this.intro = '暂时没有个人简介'
                    }
                    if (response.data.avatar){
                        this.image = this.img_url + response.data.avatar;
                    }
                    else {
                        this.imgge = 'img/widget-myphoto.jpg';
                    }
                    if (response.data.email){
                        this.email = response.data.email;
                    }
                })
                .catch(error => {
                    alert("访问个人信息失败")
                })

            axios.get(this.host + '/answer/',{
                headers: {
                    'Authorization': 'JWT ' + this.token
                },
                responseType: 'json'
                })
                .then(response=>{
                    this.questions = response.data.results
                })
                .catch(error=>{
                    alert("出错了")
                })
            axios.get(this.host + '/myanswers/',{
                headers: {
                    'Authorization': 'JWT ' + this.token
                },
                responseType: 'json'
                })
                .then(response=>{
                    this.answerses = response.data.results
                })
                .catch(error=>{
                    alert("出错了")
                })
        }

        else {
            location.href = '/login.html?'
        }

    },

    methods:{

        check_city: function () {
            this.error_city = true
        },
        check_school: function () {
            this.error_school = true
        },
        check_company: function () {
            this.error_company = true
        },
        check_web: function () {
            this.error_web = true
        },
        check_intro: function () {
            this.error_intro = true
            this.errors_intro = false
        },
        my_questions:function () {
            this.error_question = true;
            this.cls1 = 'active';
            this.cls2 = null;
            this.error_answer = false;
        },
        my_answers:function () {
            this.error_question = false;
            this.cls1 = null;
            this.cls2 = 'active';
            this.error_answer = true;
        },
        // 居住地址
        on_submit_one:function () {
            axios.put(this.host + '/city/', {
                city:this.city
            }, {
                headers: {
                    'Authorization': 'JWT ' + this.token
                },
                responseType: 'json'
            })
                .then(response=>{
                    this.error_city = false
                }).catch(error=> {
                alert("设置失败")

            })

        },
        // 毕业学校
        on_submit_two:function () {
            axios.put(this.host + '/school/', {
                school:this.school,
                specialities: this.specialities
            }, {
                headers: {
                    'Authorization': 'JWT ' + this.token
                },
                responseType: 'json'
            })
                .then(response=>{
                    this.school = response.data.school
                    this.specialities = response.data.specialities
                    this.message = this.school + '-' + this.specialities
                    this.error_school = false
                }).catch(error=> {
                alert("设置失败")

            })

        },

        // 所在公司
        on_submit_three:function () {
            axios.put(this.host + '/company/', {
                company:this.company,
                position: this.position
            }, {
                headers: {
                    'Authorization': 'JWT ' + this.token
                },
                responseType: 'json'
            })
                .then(response=>{
                    this.company = response.data.company
                    this.position = response.data.position
                    this.message_company = this.company + '-' + this.position
                    this.error_company = false
                }).catch(error=> {
                alert("设置失败")

            })

        },
        // 个人网站
        on_submit_four:function () {
            axios.put(this.host + '/website/', {
                website: this.website
            }, {
                headers: {
                    'Authorization': 'JWT ' + this.token
                },
                responseType: 'json'
            })
                .then(response=>{
                    this.website = response.data.website
                    this.error_web = false
                }).catch(error=> {
                alert("设置失败")
            })
        },
        set_intro: function () {
            axios.put(this.host + '/intro/',{
                intro:this.intro
            },{
                headers: {
                    'Authorization': 'JWT ' + this.token
                },
                responseType: 'json'
            })
                .then(response=>{
                    this.intro = response.data.intro
                    this.error_intro = false
                    this.errors_intro = true
                }).catch(error=>{
                alert("设置失败")
            })
        },
        // 修改头像
        submit: function() {
            console.log(this.$refs.form);
            console.log(this.files);

            var formData = new FormData(this.$refs.form);         //获取表单数据

            let config = {headers:{'Content-Type':'multipart/form-data','Authorization': 'JWT ' + this.token}};  //添加请求头

            axios.post(this.host + '/avatar/', formData, config)
            .then(response=>{
                this.upload = false;
                console.log(response.data);
                this.image = this.img_url + response.data.avatar;
                console.log(this.image);
            })
            .catch(error=>{
                alert('上传失败')
            })
        },

        // 退出
        logout: function(){
            sessionStorage.clear();
            localStorage.clear();
            location.href = '/login.html';
        },



        //-------------------------------------------------------//
        // 修改用户资料
        submit_userdata:function () {
            var fields = '';

            if (this.show_name){
                this.show_name = false;
                var formData = {'username':this.username};
                fields = 'username'
            }
            else if(this.show_web){
                this.show_web = false;
                var formData = {'website':this.website};
                fields = 'website'
            }
            else if(this.show_email){
                this.show_email = false;
                var formData = {'email':this.email};
                fields = 'email'
            }
            else if(this.show_mobile){
                this.show_mobile = false;
                var formData = {'mobile':this.mobile};
                fields = 'mobile'
            }
            else {
                return
            }

            axios.patch(this.host + '/userdatas/9999/', formData, {
                headers: {
                    'Authorization': 'JWT ' + this.token
                },
                responseType: 'json'
            })
                .then(response=>{
                    if (fields != 'password'){
                        if(fields == 'username'){this.username=response.data.username}
                        if(fields == 'website'){this.website=response.data.website}
                        if(fields == 'mobile'){this.mobile=response.data.mobile}
                        if(fields == 'email'){this.email=response.data.email}
                    }
                    alert('修改成功');
                }).catch(error=> {
                alert("设置失败");
            })
        },

        // 修改密码
        submit_pwd:function () {
            var formData = {
                            'password': this.password,
                            'password1': this.password1,
                            };

            axios.patch(this.host + '/userpwd/', formData, {
                headers: {
                    'Authorization': 'JWT ' + this.token
                },
                responseType: 'json'
            })
                .then(response=>{
                    alert('修改成功');
                    location.href = '/login.html';
                }).catch(error=> {
                alert("设置失败");
            })
        }

    }


});